package com.queen.testretrofitrxjava.httpSubscriber;

import com.queen.testretrofitrxjava.http.NetworkUtil;
import com.queen.testretrofitrxjava.progress.ProgressCancelListener;
import com.queen.testretrofitrxjava.progress.ProgressDialogHandler;
import com.queen.testretrofitrxjava.ui.views.UIToast;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import rx.Subscriber;

/**
 * 用于在Http请求开始时，自动显示一个ProgressDialog
 * 在Http请求结束是，关闭ProgressDialog
 * 调用者自己对请求数据进行处理
 * Created by liukun on 16/3/8.
 */
public class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {

    public static final String TAG = ProgressSubscriber.class.getSimpleName();

    private SubscriberOnNextListener<T> onNextListener;
    private ProgressDialogHandler mProgressDialogHandler;

    public ProgressSubscriber(SubscriberOnNextListener<T> onNextListener) {
        this.onNextListener = onNextListener;
        this.mProgressDialogHandler = ProgressDialogHandler.getInstance(this, true);
    }

    private void showProgressDialog(){
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog(){
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     */
    @Override
    public void onStart() {

        /**
         * 先判断网络是否连接
         */
        if (NetworkUtil.isNetworkConnected()) {
            showProgressDialog();
        } else {
            //如果没有网络连接，取消Subscriber的订阅
            if (this != null && !this.isUnsubscribed()) {
                this.unsubscribe();
            }
            UIToast.showToastShort("无网络连接，请检查您的网络状态");
        }

    }

    /**
     * 完成，隐藏ProgressDialog
     */
    @Override
    public void onCompleted() {
        UIToast.showToastShort("onCompleted");
        dismissProgressDialog();
    }

    /**
     * 对错误进行统一处理
     * 隐藏ProgressDialog
     * @param e
     */
    @Override
    public void onError(Throwable e) {

        if (e instanceof SocketTimeoutException) {
            UIToast.showToastShort("请求超时，请检查您的网络状态");
        } else if (e instanceof ConnectException) {
            UIToast.showToastShort("网络中断，请检查您的网络状态");
        } else {
            UIToast.showToastShort(e.getMessage());
        }

        dismissProgressDialog();

    }

    /**
     *
     * @param t 创建Subscriber时的泛型类型
     */
    @Override
    public void onNext(T t) {
        if (null != onNextListener) {
            onNextListener.onNext(t);
        }
    }

    /**
     * 用于取消ProgressDialog时，解除Subscriber的订阅，同时取消了正在进行的Http请求
     */
    @Override
    public void onCancelProgress() {
        if (this != null && !this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }
}
